/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.h
  * @brief          : Header for main.c file.
  *                   This file contains the common defines of the application.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __MAIN_H
#define __MAIN_H

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "stm32f3xx_hal.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "pid.h"
/* USER CODE END Includes */

/* Exported types ------------------------------------------------------------*/
/* USER CODE BEGIN ET */
    typedef enum
    {
        kOFF,
        kON,
    } switch_state_t;

    typedef enum
    {
        kOK,
        kERR,
        kRESTART,
    } error_state_t;

    typedef enum
    {
        kModule1st,
        kModule2nd,
        kModule3rd,

        kModuleNum
    } pa_module_id_t;

    typedef enum
    {
        kSubCtrl1st,
        kSubCtrl2nd,
        kSubCtrl3rd,
        kSubCtrl4th,
        kSubCtrl5th,
        kSubCtrl6th,
        kSubCtrl7th,
        kSubCtrl8th,
        kSubCtrl9th,

        kSubCtrlNum
    } sub_control_id_t;

    typedef struct
    {
        uint8_t current;
        uint8_t rf_wave;
    } adc_channel_index_t;

    typedef struct
    {
        GPIO_TypeDef *port;
        uint16_t pin;
        float *val_ptr;
    } ds18b20_config_t;

    typedef struct
    {
        GPIO_TypeDef *port;
        uint16_t pin;
    } pd_pin_index_t;

    typedef struct
    {
        DAC_HandleTypeDef *handle;
        uint32_t channel;
    } dac_channel_index_t;

    typedef struct
    {
        GPIO_TypeDef *port;
        uint16_t pin;
    } phase_nss_index_t;

    typedef struct
    {
        const adc_channel_index_t *adc_index;
        const ds18b20_config_t *temp_index;
        const pd_pin_index_t *pd_index;
        const dac_channel_index_t *dac_index;
        const phase_nss_index_t *phase_index;
    } pa_module_hardware_config_t;

    typedef struct
    {
        float current;
        float rf_wave;
        float temperature;
        float attenuation;
        float phase;
    } pa_module_t;

    typedef struct
    {
        uint8_t pa_enable : 3;
        switch_state_t auto_run : 1;
        error_state_t error : 4;
        uint8_t soft_reset_count : 8;
    } sub_control_state_t;

    typedef struct
    {
        float attenuation[kModuleNum]; // 衰减器值
        float phase[kModuleNum];       // 相位值
        pid_config_t forward_pid;
        pid_config_t reverse_pid;
    } sub_config_t;

    typedef struct
    {
        uint8_t frame; // 帧头
        uint8_t type;  // 数据类型
        uint8_t init;  // 初始化标志
        uint8_t ip[4]; // IP地址
    } sub_header_t;

    /**
     * @brief 分控制器flash数据结构体
     *
     */
    typedef struct
    {
        sub_header_t header;
        sub_config_t config;
        /*待定*/
    } __ALIGNED(4) sub_control_flash_t;
    /**
     * @brief 分控制器结构体
     *
     */
    typedef struct
    {
        sub_header_t header;
        sub_control_state_t state;  // 状态
        pa_module_t pa[kModuleNum]; // PA模块
        struct
        {
            int target;
            float forward;
            float reverse;
        } rf_power; // 射频功率
        union {
            uint8_t byte[4];
            uint16_t half[2];
            uint32_t word;
        } check;
    } sub_control_t;
/* USER CODE END ET */

/* Exported constants --------------------------------------------------------*/
/* USER CODE BEGIN EC */

/* USER CODE END EC */

/* Exported macro ------------------------------------------------------------*/
/* USER CODE BEGIN EM */

/* USER CODE END EM */

/* Exported functions prototypes ---------------------------------------------*/
void Error_Handler(void);

/* USER CODE BEGIN EFP */

/* USER CODE END EFP */

/* Private defines -----------------------------------------------------------*/
#define PA_PD1_Pin GPIO_PIN_13
#define PA_PD1_GPIO_Port GPIOC
#define PA_PD2_Pin GPIO_PIN_14
#define PA_PD2_GPIO_Port GPIOC
#define PA_PD3_Pin GPIO_PIN_15
#define PA_PD3_GPIO_Port GPIOC
#define PA_CURRENT1_Pin GPIO_PIN_0
#define PA_CURRENT1_GPIO_Port GPIOA
#define PA_CURRENT2_Pin GPIO_PIN_1
#define PA_CURRENT2_GPIO_Port GPIOA
#define PA_CURRENT3_Pin GPIO_PIN_2
#define PA_CURRENT3_GPIO_Port GPIOA
#define RF_JB1_Pin GPIO_PIN_3
#define RF_JB1_GPIO_Port GPIOA
#define PA_ATT2_Pin GPIO_PIN_4
#define PA_ATT2_GPIO_Port GPIOA
#define PA_ATT1_Pin GPIO_PIN_5
#define PA_ATT1_GPIO_Port GPIOA
#define PA_ATT3_Pin GPIO_PIN_6
#define PA_ATT3_GPIO_Port GPIOA
#define RF_JB2_Pin GPIO_PIN_0
#define RF_JB2_GPIO_Port GPIOB
#define RF_JB3_Pin GPIO_PIN_1
#define RF_JB3_GPIO_Port GPIOB
#define RF_PHASE1_Pin GPIO_PIN_2
#define RF_PHASE1_GPIO_Port GPIOB
#define FORWARD_Pin GPIO_PIN_8
#define FORWARD_GPIO_Port GPIOE
#define REVERSE_Pin GPIO_PIN_9
#define REVERSE_GPIO_Port GPIOE
#define LED_Pin GPIO_PIN_8
#define LED_GPIO_Port GPIOA
#define RF_PHASE_SCK_Pin GPIO_PIN_3
#define RF_PHASE_SCK_GPIO_Port GPIOB
#define RF_PHASE2_Pin GPIO_PIN_4
#define RF_PHASE2_GPIO_Port GPIOB
#define RF_PHASE_SDA_Pin GPIO_PIN_5
#define RF_PHASE_SDA_GPIO_Port GPIOB
#define RF_PHASE3_Pin GPIO_PIN_6
#define RF_PHASE3_GPIO_Port GPIOB
#define PA_TEMP1_Pin GPIO_PIN_7
#define PA_TEMP1_GPIO_Port GPIOB
#define PA_TEMP2_Pin GPIO_PIN_8
#define PA_TEMP2_GPIO_Port GPIOB
#define PA_TEMP3_Pin GPIO_PIN_9
#define PA_TEMP3_GPIO_Port GPIOB

/* USER CODE BEGIN Private defines */
#define ENABLE_DEBUG_MODE 0
#define DEFAULT_INITIAL_VALUE 0x01

#define UART_RS485 huart1

#define SAMPLING_RESISTOR 0.0025f
#define MAX4080_GAIN 60

// ADC配置
#define ADC_CHANNEL_NUM 8 // ADC通道
#define ADC_SUM_NUM 10    // ADC采样次数
#define DL_JC1_ADC_CHANNEL 0
#define DL_JC2_ADC_CHANNEL 1
#define DL_JC3_ADC_CHANNEL 2
#define RF_JB1_ADC_CHANNEL 3
#define RF_JB2_ADC_CHANNEL 4
#define RF_JB3_ADC_CHANNEL 5
#define FORWARD_SDADC_CHANNEL 6
#define REVERSE_SDADC_CHANNEL 7

// #define DS18B20_PIN_NUM_1 NULL
#define DS18B20_PIN_NUM_2 8
#define DS18B20_PIN_NUM_3 0
#define DS18B20_PIN_NUM_4 7

#define LED_ON() HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET)
#define LED_OFF() HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET)
#define PHASE_NSS_LOW(port, pin) HAL_GPIO_WritePin(port, pin, GPIO_PIN_RESET)
#define PHASE_NSS_HIGH(port, pin) HAL_GPIO_WritePin(port, pin, GPIO_PIN_SET)

#define PA_MODULE_ALL_BIT 0x07
#define PA_MODULE_ENABLE_BIT 0x07
#define PA_MODULE_DISABLE_BIT 0x00

#define SERIAL_FRAME_HEADER 0x35

#define RS485_FRAME_HEADER 0x34
#define SUBCTRL_BASE_ADDRESS 101
#define RS485_UNIVERSAL_ADDRESS 233
#define RS485_TYPE_QUERY 0x00
#define RS485_TYPE_SAVE_PARAMETER 0x01
#define RS485_TYPE_POWER 0x02
#define RS485_TYPE_PHASE 0x03
#define RS485_TYPE_ATTENUATION 0x04
#define RS485_TYPE_AUTO_RUN 0x05
#define RS485_TYPE_FORWAR_PID 0x06
#define RS485_TYPE_REVERSE_PID 0x07
/* USER CODE END Private defines */

#ifdef __cplusplus
}
#endif

#endif /* __MAIN_H */
